package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.Userparam;
import com.example.demo.datasource.multi.dao.accountinfomapper;
import com.example.demo.datasource.multi.dao.noticemapper;
import com.example.demo.datasource.multi.dao.updatemapper;
import com.example.demo.datasource.multi.entity.Accountinfoentity;
import com.example.demo.datasource.multi.entity.notice;
import com.example.demo.datasource.multi.entity.user;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class noticecontroller {
    @Autowired
    com.example.demo.datasource.multi.dao.noticemapper noticemapper;
    @Autowired
    accountinfomapper accountinfomapper;
    @Autowired
    com.example.demo.datasource.multi.dao.usermapper usermapper;
    @Autowired
    com.example.demo.datasource.multi.dao.updatemapper updatemapper;

    @ResponseBody
    @RequestMapping("/gettopnotice")
    public JSON gettopnotice(HttpServletRequest request){
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        log.info("获取用户信息:"+user.getUsername());

        String sql="select * from user where username = '"+user.getUsername()+"'";
        System.out.println(sql);

        com.example.demo.datasource.multi.entity.user theuser=usermapper.getuser(sql);

        String sql1="select * from notice where istop = '0' and dr = 0";
        System.out.println(sql1);

        com.example.demo.datasource.multi.entity.notice thenotice=noticemapper.gettopnotice(sql1);

        if (null == thenotice){
            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",false);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }


        if (!theuser.getNoticeid().equals(thenotice.getReadid())){

            String undecodejs = StringEscapeUtils.unescapeEcmaScript(thenotice.getContent());
//            System.out.println(undecodejs);
//            String decodehtml = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(decodejs));
//            System.out.println(decodehtml);
            String undecodejava= StringEscapeUtils.unescapeJava(undecodejs);
            thenotice.setContent(undecodejava);

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",thenotice);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;

        }else {
            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",false);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }

    }

    @ResponseBody
    @RequestMapping("/isread")
    public JSON isread(HttpServletRequest request){
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        log.info("获取用户信息:"+user.getUsername());

        String sql = "update user set noticeid = (select readid from notice where istop = '0' and dr = 0) where username = '"+user.getUsername()+"'";

        int rel = updatemapper.update(sql);
        System.out.println(rel);


        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",rel);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;

    }

    @Autowired
    com.example.demo.datasource.multi.dao.insertmapper insertmapper;

    @ResponseBody
    @RequestMapping("/createnotice")
    public JSON createnotice(HttpServletRequest request,@RequestBody Map<String,String> params){
        System.out.println(params.get("noticetext"));
        System.out.println(params.get("mdtext"));
        System.out.println(params.get("title"));

        //时间格式化格式
        SimpleDateFormat tssimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String tstime=tssimpleDateFormat.format(new Date());

        String sql1 = "update notice set istop = '1',tstime = '"+tstime+"' where istop = '0'";
        int uprel = updatemapper.update(sql1);
        System.out.println(uprel);

        String javacontent = StringEscapeUtils.escapeJava(params.get("mdtext"));
        String jscontent = StringEscapeUtils.escapeEcmaScript(javacontent);

        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String createtime=simpleDateFormat1.format(new Date());

        //时间格式化格式
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String datetime=simpleDateFormat.format(new Date());

        String readid = "notice"+datetime;

        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());

        String sql2 = "INSERT INTO notice(content,readid,creater,createtime,istop,tstime,dr,title) VALUES(" +
                "'"+jscontent+"'," +
                "'"+readid+"'," +
                "'"+user.getUsername()+"'," +
                "'"+createtime+"'," +
                "'0'," +
                "'"+createtime+"',0,'" +params.get("title")+
                "')";
        System.out.println(sql2);

        int rel= insertmapper.insert(sql2);
        System.out.println(rel);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",true);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/getnoticeList")
    public JSON getAccountList(HttpServletRequest request,@RequestBody Map<String,String> params){
        System.out.println(params.get("content"));
        System.out.println(params.get("creater"));
        System.out.println(params.get("readid"));
        String where = "";
        if (!(params.get("content") == null)){
            where = where + " and content like '%"+ params.get("content")+"%'";
        }
        if (!(params.get("creater") == null)){
            where = where + " and creater like '%"+ params.get("creater")+"%'";
        }
        if (!(params.get("readid") == null)){
            where = where + " and readid = '"+ params.get("readid")+"'";
        }

        String sql1="select * from notice where dr = 0 "+where+" order by tstime desc";
        System.out.println(sql1);

        List<notice> thenoticelist=noticemapper.getnoticelist(sql1);
//        System.out.println(thenoticelist);

        for (notice no:thenoticelist
             ) {

            String undecodejs = StringEscapeUtils.unescapeEcmaScript(no.getContent());
//            System.out.println(undecodejs);
//            String decodehtml = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(decodejs));
//            System.out.println(decodehtml);
            String undecodejava= StringEscapeUtils.unescapeJava(undecodejs);
            no.setContent(undecodejava);
        }

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",thenoticelist);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
//        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/tosearch")
    public JSON searchnotice(@RequestBody Map<String,String> params){

        System.out.println(params);
        System.out.println(params.get("content"));
        System.out.println(params.get("creater"));
        System.out.println(params.get("readid"));
        String where = "";
        if (!(params.get("content") == null)){
            where = where + " and content like '"+ params.get("content")+"'";
        }
        if (!(params.get("creater") == null)){
            where = where + " and creater like '"+ params.get("creater")+"'";
        }
        if (!(params.get("readid") == null)){
            where = where + " and readid = '"+ params.get("readid")+"'";
        }

        String sql1="select * from notice where dr=0 "+where+" order by tstime desc";
        System.out.println(sql1);

        List<notice> thenoticelist=noticemapper.getnoticelist(sql1);
//        System.out.println(thenoticelist);

        for (notice no:thenoticelist
        ) {

            String undecodejs = StringEscapeUtils.unescapeEcmaScript(no.getContent());
//            System.out.println(undecodejs);
//            String decodehtml = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(decodejs));
//            System.out.println(decodehtml);
            String undecodejava= StringEscapeUtils.unescapeJava(undecodejs);
            no.setContent(undecodejava);
        }

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",thenoticelist);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
//        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/updatenotice")
    public JSON updatenotice(HttpServletRequest request,@RequestBody Map<String,String> params){
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());

        System.out.println(params.get("readid"));
        System.out.println(params.get("mdtext"));
        System.out.println(params.get("title"));
        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String tstime=simpleDateFormat1.format(new Date());
        String content = params.get("mdtext");
        String readid = params.get("readid");
        String title = params.get("title");
        String username = user.getUsername();

        String javacontent = StringEscapeUtils.escapeJava(params.get("mdtext"));
        String jscontent = StringEscapeUtils.escapeEcmaScript(javacontent);

        String sql1 = "update notice set content = '"+jscontent+"',tstime = '"+tstime+"',tsuser = '"+username+"', title = '"+title+"' where readid = '"+readid+"'";

        int updaterel=updatemapper.update(sql1);
        System.out.println(updaterel);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",true);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
//        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/deletenotice")
    public JSON deletenotice(HttpServletRequest request,@RequestBody Map<String,String> params){
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());

        System.out.println(params.get("readid"));

        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String tstime=simpleDateFormat1.format(new Date());
        String readid = params.get("readid");
        String username = user.getUsername();
        String sql1 = "update notice set dr = 1,tstime = '"+tstime+"',tsuser = '"+username+"' where readid = '"+readid+"'";

        int updaterel=updatemapper.update(sql1);
        System.out.println(updaterel);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",true);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
//        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/settopstatus")
    public JSON settopstatus(HttpServletRequest request,@RequestBody Map<String,String> params){
        System.out.println(params);



        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());

        //时间格式化格式
        SimpleDateFormat tssimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String tstime=tssimpleDateFormat.format(new Date());

        String sql1 = "update notice set istop = '1',tstime = '"+tstime+"' where istop = '0'";
        int uprel = updatemapper.update(sql1);
        System.out.println(uprel);

        //时间格式化格式
        SimpleDateFormat tssimpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间并作为时间戳
        String tstime1=tssimpleDateFormat1.format(new Date().getTime() + 1000);

        String sql = "update notice set istop = '"+params.get("newStatus")+"',tstime = '"+tstime1+"' where id = "+params.get("id");
        System.out.println(sql);
        int rel = updatemapper.update(sql);
        System.out.println(rel);

//        System.out.println(params.get("readid"));
//
//        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //获取当前时间并作为时间戳
//        String tstime=simpleDateFormat1.format(new Date());
//        String readid = params.get("readid");
//        String username = user.getUsername();
//        String sql1 = "update notice set dr = 1,tstime = '"+tstime+"',tsuser = '"+username+"' where readid = '"+readid+"'";
//
//        int updaterel=updatemapper.update(sql1);
//        System.out.println(updaterel);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",true);
        result5.put("message","提交成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
//        System.out.println(result8);

        return result8;
    }


}
